﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;

namespace ResxCrunch.Business.Managers
{
    /// <summary>
    /// Newsletter related functionality
    /// </summary>
    public class NewsletterMngr
    {
        //#region Constants
        ///// <summary>
        ///// Logger declaration
        ///// </summary>
        //private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        ///// <summary>
        ///// Create an instance og the class. 
        ///// Because the field is marked read-only it should initialize when first called
        ///// </summary>
        //private static readonly NewsletterMngr instance = new NewsletterMngr();
        //#endregion Constants

        //#region Attributes
        //#endregion Attributes

        //#region Initialization
        ///// <summary>
        ///// Prevents a default instance of the NewsletterMngr class from being created.
        ///// </summary>
        //private NewsletterMngr()
        //{
        //}

        ///// <summary>
        ///// Gets the reference to the object here.
        ///// </summary>
        //public static NewsletterMngr Instance
        //{
        //    get
        //    {
        //        // Is created during type load - thus no further checking necessary...
        //        return instance;
        //    }
        //}
        //#endregion Initialization

        //#region Public Methods
        ///// <summary>
        ///// Add newsletter to db
        ///// </summary>
        ///// <param name="newsletter">The newsletter that needs to be added.</param>
        ///// <param name="connStr">The conn string.</param>
        ///// <returns>True on success</returns>
        //public bool Add(Newsletter newsletter, string connStr)
        //{
        //    if (newsletter == null)
        //    {
        //        log.Error("Cannot add null newsletter!");
        //        return false;
        //    }

        //    try
        //    {
        //        ResxCrunchDataContext db = new ResxCrunchDataContext(connStr);

        //        db.Newsletters.InsertOnSubmit(newsletter);
        //        db.SubmitChanges();

        //        log.InfoFormat(string.Format("Newsletter '{0}' with ID '{1}' successfully added to database!", newsletter.Email, newsletter.ID));
        //        return true;
        //    }
        //    catch (Exception ex)
        //    {
        //        log.Error(string.Format("Error adding newsletter '{0}' with ID '{1}'", newsletter.Email, newsletter.ID), ex);

        //        return false;
        //    }
        //}

        ///// <summary>
        ///// Returns newsletter from DB
        ///// </summary>
        ///// <param name="newsletter">The newsletter.</param>
        ///// <param name="connStr">The conn STR.</param>
        ///// <returns>null on error</returns>
        //public Newsletter GetItem(Newsletter newsletter, string connStr)
        //{
        //    if (newsletter == null)
        //    {
        //        log.Error("Newsletter cannot be null. No newsletter returned.");
        //        return null;
        //    }

        //    return this.GetItem(newsletter.ID, connStr);
        //}

        ///// <summary>
        ///// Returns newsletter by ID
        ///// </summary>
        ///// <param name="newsletterID">The newsletter ID.</param>
        ///// <param name="connStr">The conn STR.</param>
        ///// <returns>Null on error</returns>
        //public Newsletter GetItem(int newsletterID, string connStr)
        //{
        //    try
        //    {
        //        List<Newsletter> newsletters = this.Search(newsletterID, string.Empty, connStr);

        //        if (newsletters.Count<Newsletter>() == 1)
        //        {
        //            return newsletters.Single<Newsletter>();
        //        }

        //        return null;
        //    }
        //    catch (Exception ex)
        //    {
        //        log.Error(string.Format("Error getting newsletter with id '{0}'", newsletterID), ex);
        //        return null;
        //    }
        //}

        ///// <summary>
        ///// Gets all newsletters from the system.
        ///// </summary>
        ///// <param name="connStr">The conn STR.</param>
        ///// <returns>List of all newsletters in the system</returns>
        //public List<Newsletter> GetAllItems(string connStr)
        //{
        //    return this.Search(-1, string.Empty, connStr);
        //}

        ///// <summary>
        ///// Gets newsletter by email
        ///// </summary>
        ///// <param name="email">The email address.</param>
        ///// <param name="connStr">The conn STR.</param>
        ///// <returns>List of all newsletters found in the system</returns>
        //public List<Newsletter> GetItemByEmail(string email, string connStr)
        //{
        //    return this.Search(-1, email, connStr);
        //}

        ///// <summary>
        ///// Searches the newsletters by the specified newsletter ID and email.
        ///// </summary>
        ///// <param name="newsletterID">The newsletter ID.</param>
        ///// <param name="email">The email address.</param>
        ///// <param name="connStr">The conn STR.</param>
        ///// <returns>List of newsletter</returns>
        //public List<Newsletter> Search(int newsletterID, string email, string connStr)
        //{
        //    try
        //    {
        //        ResxCrunchDataContext db = new ResxCrunchDataContext(connStr);

        //        var newsletters = from newsletter in db.Newsletters
        //                          where (newsletter.ID == newsletterID || newsletterID == -1) &&
        //                          (newsletter.Email.ToUpper() == email.ToUpper() || email.Length == 0)
        //                          select newsletter;

        //        return newsletters.ToList<Newsletter>();
        //    }
        //    catch (Exception ex)
        //    {
        //        log.Error(string.Format("Error getting newsletters with id '{0}' and email '{1}'", newsletterID, email), ex);
        //        return null;
        //    }
        //}

        ///// <summary>
        ///// Modifies the newsletter
        ///// </summary>
        ///// <param name="newsletter">The newsletter that needs to be modified.</param>
        ///// <param name="connStr">The conn STR.</param>
        ///// <returns>True on success</returns>
        //public bool Modify(Newsletter newsletter, string connStr)
        //{
        //    if (newsletter == null)
        //    {
        //        log.Error("Cannot modify newsletter");
        //        return false;
        //    }

        //    try
        //    {
        //        ResxCrunchDataContext db = new ResxCrunchDataContext(connStr);

        //        Newsletter oldNewsletter = this.GetItem(newsletter, connStr);
        //        if (oldNewsletter == null)
        //        {
        //            log.ErrorFormat("Could not retrieve newsletter '{0}' for modification", newsletter.ID);
        //            return false;
        //        }

        //        oldNewsletter.Email = newsletter.Email;
        //        oldNewsletter.Date = newsletter.Date;

        //        db.SubmitChanges();

        //        return true;
        //    }
        //    catch (Exception ex)
        //    {
        //        log.Error(string.Format("Error modifying newsletter with ID '{0}'.", newsletter.ID), ex);
        //        return false;
        //    }
        //}

        ///// <summary>
        ///// Deletes the specified newsletter.
        ///// </summary>
        ///// <param name="newsletterID">The newsletter ID.</param>
        ///// <param name="connStr">The conn STR.</param>
        ///// <returns>True on success</returns>
        //public bool Delete(int newsletterID, string connStr)
        //{
        //    try
        //    {
        //        ResxCrunchDataContext db = new ResxCrunchDataContext(connStr);

        //        var newsletters = from newsletter in db.Newsletters
        //                          where (newsletter.ID == newsletterID)
        //                          select newsletter;

        //        Newsletter deletableNewsletter = newsletters.FirstOrDefault<Newsletter>();

        //        if (deletableNewsletter != null)
        //        {
        //            db.Newsletters.DeleteOnSubmit(deletableNewsletter);
        //            db.SubmitChanges();
        //        }
        //        else
        //        {
        //            log.ErrorFormat("Could not find newsletter '{0}' for deletion.", newsletterID);
        //            return false;
        //        }

        //        return true;
        //    }
        //    catch (Exception ex)
        //    {
        //        log.Error(string.Format("Error deleting newsletter with id '{0}'.", newsletterID), ex);
        //        return false;
        //    }
        //}
        //#endregion Public Methods
    }
}
